package com.hoow.shakoow.ai.components.processBar

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat
import com.hoow.shakoow.R

class GradientTextProcessBar : View {

    //进度条颜色（渐变色的2个点）
    private val mColors = intArrayOf(Color.RED, Color.MAGENTA)

    //进度条默认颜色
    private var backgroundColor = Color.GRAY

    //文本颜色
    private var textColor = Color.RED

    //画笔
    private var mPaint: Paint

    //进度值
    private var mProgressValue = 50


    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    @SuppressLint("Recycle")
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
    ) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {
        attrs?.let {
            context?.let {
                it.obtainStyledAttributes(attrs, R.styleable.GradientProcessBar).apply {
                    mColors[0] = getColor(
                        R.styleable.GradientProcessBar_aiProcessBarStartColor,
                        ContextCompat.getColor(context, R.color.main_red)
                    )

                    mColors[1] = getColor(
                        R.styleable.GradientProcessBar_aiProcessBarEndColor,
                        ContextCompat.getColor(context, R.color.main_green)
                    )

                    backgroundColor = getColor(
                        R.styleable.GradientProcessBar_aiProcessBarBackgroundColor,
                        ContextCompat.getColor(
                            context,
                            R.color.color_ai_card_background
                        )
                    )

                    textColor = getColor(
                        R.styleable.GradientProcessBar_aiProcessBarTextColor,
                        ContextCompat.getColor(
                            context,
                            R.color.main_green
                        )
                    )
                }
            }
        }

        // 设置画笔
        mPaint = Paint()
        mPaint.isAntiAlias = true
        mPaint.style = Paint.Style.STROKE
        mPaint.strokeCap = Paint.Cap.ROUND
        mPaint.color = Color.TRANSPARENT

        // 设置进度
        mProgressValue = 100
    }

    /** 设置进度 */
    fun setProgressValue(progressValue: Int) {
        mProgressValue = progressValue.coerceAtMost(100)
        println("cur process: $mProgressValue")
        postInvalidate()
    }

    /** 设置渐变色 */
    fun setColors(colors: IntArray) {
        if (colors.size >= 2) {
            mColors[0] = colors[0]
            mColors[1] = colors[1]
            println("cur colors: $colors")
        }
    }

//    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
//
//        var width = 0
//        var height = 0
//
//        //设置宽度
//        var specMode = MeasureSpec.getMode(widthMeasureSpec)
//        var specSize = MeasureSpec.getSize(widthMeasureSpec)
//        when (specMode) {
//            MeasureSpec.EXACTLY -> {
//                width = specSize
//            }
//            MeasureSpec.AT_MOST -> {
//                width = paddingLeft + paddingRight
//            }
//            else -> {
//                width = specSize
//            }
//        }
//
//        //设置高度
//        specMode = MeasureSpec.getMode(heightMeasureSpec)
//        specSize = MeasureSpec.getSize(heightMeasureSpec)
//        when (specMode) {
//            MeasureSpec.EXACTLY -> {
//                height = specSize
//            }
//            MeasureSpec.AT_MOST -> {
//                height = width / 10
//            }
//            else -> {
//                height = specSize
//            }
//        }
//
//        //重新设置宽高
//        setMeasuredDimension(width, height)
//    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val mWidth = measuredWidth
        val mHeight = measuredHeight

        //按比例计算进度条各部分的值
        val unit = ((mWidth) / 300).coerceAtMost((mHeight) / 30)

        //线粗
        val lineWidth = 5F * unit
        mPaint.strokeWidth = lineWidth

        val innerCircleDiameter = 6 * unit//内圆直径
        val outerCircleDiameter = 10 * unit//外圆直径
        val wordHeight = 12 * unit//字高//9*unit
        // val wordWidth = 26*unit//字长
        val offsetLength = 5 * unit//留空
        // val width = 300*unit//绘画区域的长度
        val height = 30 * unit//绘画区域的高度
        val progressWidth = 258 * unit//绘画区域的长度

        val offsetHeight = height * 1F / 2
        val offsetWidth = offsetLength * 1F

        var section = (mProgressValue * 1F) / 100
        if (section > 1) section = 1F

        //底部灰色背景，指示进度条总长度
        mPaint.shader = null
        mPaint.color = backgroundColor
        canvas.drawLine(
            offsetWidth + section * progressWidth,
            offsetHeight,
            offsetWidth + progressWidth,
            offsetHeight,
            mPaint
        )

        //设置渐变色区域
        val shader = LinearGradient(
            0F,
            0F,
            offsetWidth * 2 + progressWidth,
            0F,
            mColors,
            null,
            Shader.TileMode.CLAMP
        )
        mPaint.shader = shader

        //画出渐变色进度条
        canvas.drawLine(
            offsetWidth,
            offsetHeight,
            offsetWidth + section * progressWidth,
            offsetHeight,
            mPaint
        )

        //渐变色外圆
        mPaint.strokeWidth = 1F
        mPaint.style = Paint.Style.FILL
        canvas.drawCircle(
            offsetWidth + section * progressWidth,
            offsetHeight,
            outerCircleDiameter * 1F / 2,
            mPaint
        )

        //绘制两条斜线，使外圆到进度条的连接更自然
        if (section * 100 > 1.8) {
            mPaint.strokeWidth = 2F * unit
            canvas.drawLine(
                offsetWidth + section * progressWidth - 6 * unit,
                offsetHeight - (1.5F * unit),
                offsetWidth + section * progressWidth - 1 * unit,
                offsetHeight - (3.8F * unit),
                mPaint
            )
            canvas.drawLine(
                offsetWidth + section * progressWidth - 6 * unit,
                offsetHeight + (1.5F * unit),
                offsetWidth + section * progressWidth - 1 * unit,
                offsetHeight + (3.8F * unit),
                mPaint
            )
        }

        //白色内圆
        mPaint.shader = null
        mPaint.color = Color.WHITE
        canvas.drawCircle(
            offsetWidth + section * progressWidth,
            offsetHeight,
            innerCircleDiameter * 1F / 2,
            mPaint
        )

        //绘制文字--百分比
        mPaint.strokeWidth = 2F * unit
        mPaint.color = textColor
        mPaint.textSize = wordHeight * 1F

        //计算坐标使文字居中
        val fontMetrics: Paint.FontMetrics = mPaint.fontMetrics
        val fontHeight = fontMetrics.bottom - fontMetrics.top
        val baseY = height / 2 + fontHeight / 2 - fontMetrics.bottom
        canvas.drawText(
            "$mProgressValue%",
            progressWidth + 2 * offsetWidth,
            baseY,
            mPaint
        )
    }
}